﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SitePages_frmClientItemSearch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(ItemComboBox.SelectedIndex > Constants.INVALID_ID && validateInput())
            loadReportTable(Convert.ToInt32(ClientComboBox.SelectedValue), Convert.ToInt32(ItemComboBox.SelectedValue));
        loadItemComboBox();
        loadClientComboBox();
    }

    /// <summary>
    /// Carga el combo box con los artículos que tiene disponible la empresa
    /// </summary>
    private void loadItemComboBox()
    {
        ItemComboBox.Items.Clear();
        WebServiceAgent userAgent = (WebServiceAgent)Session[Constants.USER_WEB_SERVICE_CONN];
        ERPSIA.Item[] items = userAgent.GetAvailableItems();
        ItemComboBox.Items.Add(new ListItem("Ninguno", Constants.INVALID_ID.ToString()));
        foreach (ERPSIA.Item item in items)
            ItemComboBox.Items.Add(new ListItem(item.CodArticulo + " - " + item.Descripción, item.Id.ToString()));
    }

    /// <summary>
    /// Carga el combo box con los clientes que están registrados en la empresa
    /// </summary>
    private void loadClientComboBox()
    {
        ClientComboBox.Items.Clear();
        WebServiceAgent userAgent = (WebServiceAgent)Session[Constants.USER_WEB_SERVICE_CONN];
        ERPSIA.AuxiliarAccount[] clients = userAgent.GetClients();
        ClientComboBox.Items.Add(new ListItem("Ninguno", Constants.INVALID_ID.ToString()));
        foreach (ERPSIA.AuxiliarAccount client in clients)
            ClientComboBox.Items.Add(new ListItem(client.OwnerName, client.AccountID.ToString()));
    }

    /// <summary>
    /// Carga la tabla de reportes de cliente por artículo o artículo por cliente
    /// </summary>
    /// <param name="auxId">Id del cliente para el que se quiere reporte. Si no se quiere este filtro enviar un -1</param>
    /// <param name="itemId">Id del artículo para el que se quiere reporte. Si no se quiere este filtro enviar un -1</param>
    private void loadReportTable(int auxId, int itemId)
    {
        WebServiceAgent userAgent = (WebServiceAgent)Session[Constants.USER_WEB_SERVICE_CONN];
        ERPSIA.SaleReport[] reports = userAgent.GetClientInformation(auxId, itemId);
        foreach (ERPSIA.SaleReport report in reports)
        {
            ERPSIA.AuxiliarAccount client = FinderManager.GetClient(report.ClientId, userAgent.GetClients());
            ERPSIA.Item item = FinderManager.GetItem(report.ItemId, userAgent.GetAvailableItems());
            TableRow row = new TableRow();
            TableCell clientCell = new TableCell();
            clientCell.Text = client.OwnerName;
            TableCell itemCodeCell = new TableCell();
            itemCodeCell.Text = item.CodArticulo;
            TableCell descriptionCell = new TableCell();
            descriptionCell.Text = item.Descripción;
            TableCell lastSaleCell = new TableCell();
            lastSaleCell.Text = report.LastSaleDate.ToString();
            TableCell quantityTotalCell = new TableCell();
            quantityTotalCell.Text = report.TotalQuantity.ToString("0.00") + " " + item.MeasureAcronim;
            TableCell soldTotalCell = new TableCell();
            soldTotalCell.Text = "₡ " + report.TotalSold.ToString("0.00");
            TableCell invoiceAverageCell = new TableCell();
            invoiceAverageCell.Text = "₡ " + Math.Round(report.InvoiceAverageSold, 2).ToString("0.00");
            HyperLink link = new HyperLink();
            link.Text = "Ver historial";
            link.NavigateUrl = "~/SitePages/frmHistorial.aspx?clientID=" + client.AccountID.ToString() + "&itemID=" + item.Id.ToString();

            TableCell historyCell = new TableCell();
            historyCell.Controls.Add(link);
            row.Cells.Add(clientCell);
            row.Cells.Add(itemCodeCell);
            row.Cells.Add(descriptionCell);
            row.Cells.Add(lastSaleCell);
            row.Cells.Add(quantityTotalCell);
            row.Cells.Add(soldTotalCell);
            row.Cells.Add(invoiceAverageCell);
            row.Cells.Add(historyCell);
            ItemTable.Rows.Add(row);
            foreach (TableCell cell in row.Cells)
                cell.HorizontalAlign = HorizontalAlign.Center;
        }
    }

    private bool validateInput()
    {
        return !(Convert.ToInt32(ItemComboBox.SelectedValue) == Constants.INVALID_ID && Convert.ToInt32(ClientComboBox.SelectedValue) == Constants.INVALID_ID);
    }

    protected void ClientSearchButton_Click(object sender, EventArgs e)
    {
    }
}